package controllers.cart;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lib_model.Account.Account;
import lib_model.Cart.Cart;
import lib_model.Cart.ItemCart;
import lib_model.Customer.Customer;
import lib_model.Order.Orders_bill;
import lib_model.Order.Orders_bill_details;

/**
 * Servlet implementation class Checkout
 */
@WebServlet("/Cart/CheckOut")

public class Checkout extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Checkout() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Cart c = (Cart) request.getSession().getAttribute("Cart");
		Orders_bill bill = new Orders_bill();
		Customer cus = new Customer();
		cus.username_ref = ((Account) request.getSession().getAttribute("User")).username;
		cus.location = "";
		cus.name = "";
		int id = 0;
		if(cus.GetInformation().ObjResult == null){
			id = Integer.parseInt(cus.Add().ObjResult.toString());
		}else{
			id = ((Customer)cus.GetInformation().ObjResult).id;
		}
		bill.orders_bill_type_name_ref = "Processed";
		bill.customer_id_ref = id;
		bill.account_warehouse_username_ref = "warehouse_one";
		bill.warehouse_id_ref = 1;
		int r  = Integer.parseInt(bill.Add().ObjResult.toString());
		for(ItemCart item : c.getListCart() ){
			Orders_bill_details billd = new Orders_bill_details();
			billd.orders_bill_id_ref = r;
			billd.product_id_ref = item.id;
			billd.quantity = item.quantity;
			billd.Add();
		}
		
		response.getWriter().write("Suscess !!! Click here to home");
		response.getWriter().flush();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
